package pw.hsu.messagerelayer.view.activity;


import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import pw.hsu.messagerelayer.R;
import pw.hsu.messagerelayer.utils.SharedPreferenceUtil;

public class UrlRelayerActivity extends AppCompatActivity implements
        CompoundButton.OnCheckedChangeListener, View.OnClickListener {

    private Switch mUrlSwitch;
    private RelativeLayout mLayoutUrlHost, mLayoutUrlTemplate;
    private TextView mTextUrlHost, mTextUrlTemplate;
    private Integer showLen = 38;

    private SharedPreferenceUtil mSharedPreferenceUtil;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_url_relayer);
        initActionbar();

        this.mSharedPreferenceUtil = new SharedPreferenceUtil(this);
        initView();
        initData();
        initListener();
    }

    private void initActionbar() {
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    private void initData() {

        mUrlSwitch.setChecked(mSharedPreferenceUtil.getUrlRelay());

        mTextUrlHost.setText(mSharedPreferenceUtil.getUrlHost());
        mTextUrlTemplate.setText(mSharedPreferenceUtil.getUrlTemplate().replace('\n', ' '));
    }


    private void initView() {
        mUrlSwitch = (Switch) findViewById(R.id.switch_url);

        mLayoutUrlHost = (RelativeLayout) findViewById(R.id.layout_url_host);
        mLayoutUrlTemplate = (RelativeLayout) findViewById(R.id.layout_url_template);

        mTextUrlHost = (TextView) findViewById(R.id.text_url_host);
        mTextUrlTemplate = (TextView) findViewById(R.id.text_url_template);
    }

    private void initListener() {
        mLayoutUrlHost.setOnClickListener(this);
        mLayoutUrlTemplate.setOnClickListener(this);

        mUrlSwitch.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.switch_url:
                mSharedPreferenceUtil.setUrlRelay(isChecked);
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_url_host:
                showUrlHostDialog();
                break;
            case R.id.layout_url_template:
                showUrlTemplateDialog();
                break;
        }
    }

    private void showUrlHostDialog() {

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View view = LayoutInflater.from(this).inflate(R.layout.dialog_url_host, null, false);
        final EditText urlHostEdit = (EditText) view.findViewById(R.id.dialog_url_edit);

        String urlHost = mSharedPreferenceUtil.getUrlHost();
        if (!urlHost.equals("请求地址")) {
            urlHostEdit.setText(urlHost);
        }

        builder.setView(view);
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mSharedPreferenceUtil.setUrlHost(urlHostEdit.getText().toString());
                mTextUrlHost.setText(urlHostEdit.getText());
            }
        });
        builder.show();
    }

    private void showUrlTemplateDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View view = LayoutInflater.from(this).inflate(R.layout.dialog_url_template, null, false);
        final EditText urlHostTemplateEdit = (EditText) view.findViewById(R.id.dialog_url_template_edit);

        String urlTemplate = mSharedPreferenceUtil.getUrlTemplate();
        if (!urlTemplate.equals("参数模板")) {
            urlHostTemplateEdit.setText(urlTemplate);
        }

        builder.setView(view);
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mSharedPreferenceUtil.setUrlTemplate(urlHostTemplateEdit.getText().toString());
                mTextUrlTemplate.setText(urlHostTemplateEdit.getText().toString().replace('\n', ' '));
            }
        });
        builder.show();
    }
}
